/*
 *  $Id: Pray.cs 1063 2010-05-20 17:40:43Z thenn.erannor $
 *  This file is a part of Ragima CCG.
 *  (C) 2008 Ragima development group.
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Cards001.Light.Abilities {
    public class Pray: AbilityCard {
        public Pray(): base("001_l_a_pray") {
            Level = CompleteLevel.ReadyForTest;
            Element = ElementType.Light;
            AllowInSinner = true;
        }

        public override void DoAction(TargetArgs args) {
            args.Player.IncrementMana(ElementType.Light, Spells.DivinePresence.GetCoefficient(args.Player));
            Creature prayer = args.Caster as Creature;
            if (prayer == null) return;
            RemoveSinner(prayer);
        }

        public static void RemoveSinner(Creature prayer) {
            prayer.RemoveAffect<Affects.Sinner>();
            prayer.RemoveAffect<Affects.Sorrow>();
        }

        public class OnPray: Affects.Event<CreatureEvent> {
            public OnPray(CreatureEvent handler): base(handler, AffectSummaryType.Local) {}

            public override string Id {
                get { return AffectManager.GetCardId<Pray>("event"); }
            }

            public override void SetEvent(AffectSummary summary) {
                summary.OnUseAbility += DoAbility;
            }
            public override void ResetEvent(AffectSummary summary) {
                summary.OnUseAbility -= DoAbility;
            }

            private void DoAbility(AbilityCard ability, TargetArgs args) {
                if (ability is Pray && Handler != null && args.Caster is Creature)
                    Handler((Creature) args.Caster);
            }

            public override Affect Clone() {
                return new OnPray(Handler);
            }
        }

        public class PrayBonus: OnPray {
            public PrayBonus(CreatureEvent handler): base(handler) {}

            public override Affect Clone() {
                return new PrayBonus(Handler);
            }
        }

        public abstract class PrayEnchantmentCard: TargetEnchantmentCard<Creature> {
            public PrayEnchantmentCard(string id): base(id) {
                Target = new CreatureTarget(GetTargetPhrase(), true, true, IsPrayer);
            }

            private static bool IsPrayer(TargetRequest request, Creature cr) {
                return cr.GetAbilities().Exists(IsPray);
            }
            private static bool IsPray(AbilityCard ability) {
                return ability is Pray;
            }

            private static int GetPrayBonusCount(Creature cr) {
                int count = 0;
                foreach (Affect affect in cr.Summary.GetEnchantments())
                    if (affect is GroupAffect)
                        foreach (Affect sub in (GroupAffect) affect)
                            if (sub is PrayBonus) {
                                count++;
                                break;
                            }
                return count;
            }

            private static int GetPrayBonusCost(TargetArgs<Creature> args) {
                // считаем добавочную цену от числа бонусов на цели
                return GetPrayBonusCount(args.Target) * 2;
            }

            protected override void ManaCostAction(TargetArgs<Creature> args, ElementValues manas) {
                manas.Increment(ElementType.Light, GetPrayBonusCost(args));
            }
        }
    }
}
